<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/6/13
 * Time: 15:26
 */
namespace app\api\model;
use think\Model;

class Search extends Model {
    protected $table='up_user_search';

    /**
     * @param $uid 用户ID
     * @return false|\PDOStatement|string|\think\Collection
     *  查询用户的搜索记录
     */
    public function History($uid,$type){
        $model = self::instance();
        $where['search_user_id'] = $uid;
        $where['search_type'] = $type;
        $list = $model->where($where)->order('search_datetime desc')->limit(5)->select();
        return $list;
    }

    /**
     * @param $uid
     * @return false|\PDOStatement|string|\think\Collection
     * 清除用户所有的历史记录
     */
    public function clearAll($uid,$type){
        $model = self::instance();
        $where['search_user_id'] = $uid;
        $where['search_type'] = $type;
        $list = $model->where($where)->delete();
        return $list;
    }
    public function clear($where){
        $model = self::instance();
        $list = $model->where($where)->delete();
        return $list;
    }
    /**
     * @param $where
     * @return mixed
     * 查询一条记录
     */
    public function selectOne($where){
        $model = self::instance();
        $list = $model->where($where)->value('search_id');
        return $list;
    }

    /**
     * @param $data
     * @return false|int
     * 用户搜索历史记录写入数据库
     */
    public function addList($data){
        $model = self::instance();
        $list = $model->save($data);
        return $list;

    }
    private static $instance;
    public static function instance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}